import { saveCustAddress, getCustAddressList, deleteShopCartById, updateCustAddressById } from "../../../api/custBaseController";
Page({
  /**
   * 页面的初始数据
   */
  data: {
    addressInfo:{},
    addressList:[]   
  },
  onShow(){
    this.getCustAddressList()
  },
  /**
   * 修改 删除
   */
  handleClick(e){
    console.log(e);
    let { type, id } = e.currentTarget.dataset
    if (type === 'update') {
      let self = this
      wx.chooseAddress({
        success(res) {
          self.setData({
            ['addressInfo.contact']: res.userName,
            ['addressInfo.mobile']: res.telNumber,
            ['addressInfo.province']: res.provinceName,
            ['addressInfo.city']: res.cityName,
            ['addressInfo.county']: res.countyName,
            ['addressInfo.address']: res.detailInfo,
            ['addressInfo.postalCode']: res.postalCode,
          })
          console.log(res);
          self.updateCustAddressById(id)
        }
      })
    }else if (type === 'del') {
      this.deleteShopCartById(id)
    }
  },
  /**
   * 根据Id修改客户地址
   */
  async updateCustAddressById(id){
    let resolve = await updateCustAddressById({
      id: id,
      ...this.data.addressInfo
    })
  },
  /**
   * 删除列表
   */
  async deleteShopCartById(id){
    let resolve = await deleteShopCartById({
      id: id
    })
    if (resolve.success) {
      wx.showToast({
        title: '删除成功！',
        mask: true,
        success: (res)=>{
          setTimeout(() => {
            this.getCustAddressList()
          }, 1500);
        }
      })
    }
  },
  /**
   * 地址列表
   */
  async getCustAddressList(){
    let resolve = await getCustAddressList({
      custId: wx.getStorageSync('custId'),
    })
    this.setData({
      addressList: resolve.data.list
    })
  },
  /**
   * 调用微信地址功能
   */
  openAddress(e) {
    let { item, type } = e.currentTarget.dataset
    let self = this
    if (type === 'add') {
      wx.chooseAddress({
        success(res) {
          self.setData({
            ['addressInfo.contact']: res.userName,
            ['addressInfo.mobile']: res.telNumber,
            ['addressInfo.province']: res.provinceName,
            ['addressInfo.city']: res.cityName,
            ['addressInfo.county']: res.countyName,
            ['addressInfo.address']: res.detailInfo,
            ['addressInfo.postalCode']: res.postalCode,
          })
          self.saveCustAddress()
        }
      })
    }else{
      let pages = getCurrentPages();//获取页面栈
      let prevPage = pages[pages.length - 2]; //上一个页面
      //直接调用上一个页面的setData()方法，把数据存到上一个页面pages/index/index/index中去
      prevPage.setData({
        addressInfo: item
      })
      wx.navigateBack({
        delta: 1,
      })
    }

  },
    /**
   * 调用微信地址功能
   */
  openAddress2() {
    let self = this
    wx.chooseAddress({
      success(res) {
        self.setData({
          ['addressInfo.contact']: res.userName,
          ['addressInfo.mobile']: res.telNumber,
          ['addressInfo.province']: res.provinceName,
          ['addressInfo.city']: res.cityName,
          ['addressInfo.county']: res.countyName,
          ['addressInfo.address']: res.detailInfo,
          ['addressInfo.postalCode']: res.postalCode,
        })
        console.log(res);
        self.saveCustAddress()
      }
    })
  },
  /**
   * 添加到客户地址
   */
  async saveCustAddress(){
    // let { contact, mobile, province, city, county, address, postalCode } = this.data.addressInfo
    let resolve = await saveCustAddress({
      custId: wx.getStorageSync('custId'),
      ...this.data.addressInfo
    })
  }
})